package com.example.springboot.service.admin.Impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.mapper.CommentMapper;
import com.example.springboot.mapper.ContentsMapper;
import com.example.springboot.mapper.MetasMapper;
import com.example.springboot.mapper.TagMapper;
import com.example.springboot.model.entity.Comment;
import com.example.springboot.model.entity.Contents;
import com.example.springboot.model.entity.Metas;
import com.example.springboot.model.entity.Tag;
import com.example.springboot.model.form.PageUtil;
import com.example.springboot.model.form.Result;
import com.example.springboot.model.req.admin.ContentsReq;
import com.example.springboot.model.vo.CommentVO;
import com.example.springboot.model.vo.ContentsVO;
import com.example.springboot.model.vo.MetasVO;
import com.example.springboot.service.admin.ContentsService;
import com.example.springboot.utils.CopyUtil;
import com.example.springboot.utils.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zf
 * @文章表
 * @since 2021-03-17
 */
@Service
public class ContentsServiceImpl extends ServiceImpl<ContentsMapper, Contents> implements ContentsService {

    @Resource
    CommentMapper commentMapper;

    @Resource
    MetasMapper metasMapper;

    @Resource
    TagMapper tagMapper;

    @Override
    public Result getPage(ContentsReq req) {
        IPage<Contents> ipage = page(req.toPage(), new LambdaQueryWrapper<Contents>()
                .eq(Contents::getInvalid, 0)
                .like(StringUtil.isNotBlank(req.getTitle()), Contents::getTitle, req.getTitle())
                .eq(req.getStatus() != null, Contents::getStatus, req.getStatus())
                .orderByDesc(Contents::getCreateTime));

        Page<Contents> page = PageUtil.toPage(ipage);
        return Result.buildSuccess(PageUtil.convert(page, e -> {
            //主表复制元素
            ContentsVO vo = CopyUtil.copyObject(e, ContentsVO.class);
            //评论数据
            List<Comment> comments = commentMapper.selectList(new LambdaQueryWrapper<Comment>()
                    .eq(Comment::getInvalid, 0)
                    .eq(Comment::getCid, e.getId())
                    .eq(Comment::getType,1));
            List<CommentVO> commentVOS = CopyUtil.copyList(comments, CommentVO.class);
            vo.setCommentVOList(commentVOS);

            //分类
            Metas meta = metasMapper.selectById(e.getCategorieId());
            MetasVO metasVO = CopyUtil.copyObject(meta, MetasVO.class);
            vo.setMetasVO(metasVO);

            if (StringUtil.isNotBlank(e.getTagsId())) {
                List<String> list = new ArrayList<>();
                //标签
                List jsonArray = JSON.parseArray(e.getTagsId());
                for (Object id : jsonArray) {
                    Tag tag = tagMapper.selectById((Integer) id);
                    list.add(tag.getName());
                }
                vo.setTagsId(list.toString());
            }
            return vo;
        }));
    }

    @Override
    public Result addInfo(ContentsReq req) {
        //主表复制元素
        Contents contents = CopyUtil.copyObject(req, Contents.class);
        boolean flag;
        if (req.getId() != null) {
            flag = updateById(contents);
        } else {
            flag = save(contents);
        }
        if (flag) {
            return Result.buildSuccess();
        }
        return Result.buildFail();
    }

    @Override
    public Result getInfo(Integer id) {
        Contents byId = getById(id);
        //主表复制元素
        ContentsVO vo = CopyUtil.copyObject(byId, ContentsVO.class);
        //评论数据
        List<Comment> comments = commentMapper.selectList(new LambdaQueryWrapper<Comment>()
                .eq(Comment::getInvalid, 0)
                .eq(Comment::getType,1)
                .eq(Comment::getCid, byId.getId()));
        List<CommentVO> commentVOS = CopyUtil.copyList(comments, CommentVO.class);
        vo.setCommentVOList(commentVOS);

        //分类
        Metas meta = metasMapper.selectById(byId.getCategorieId());
        MetasVO metasVO = CopyUtil.copyObject(meta, MetasVO.class);
        vo.setMetasVO(metasVO);
        return Result.buildSuccess(vo);
    }

    @Override
    public Result deleteInfos(String ids) {
        if (update(new UpdateWrapper<Contents>().lambda()
                .set(Contents::getInvalid, 1)
                .inSql(Contents::getId, ids)
        )) {
            return Result.buildSuccess();
        }
        return Result.buildFail();
    }

}
